package controller;

import dao.ClassificationDao;
import dao.GoodsDao;
import dao.SupplierDao;
import domain.Classification;
import domain.Goods;
import domain.Supplier;
import domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import util.ClassifiedGoods;
import util.CompositeSupplier;
import util.GoodsOfSupplier;
import util.JsonResult;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/supplier")
public class SupplierController {
    @Resource
    private SupplierDao supplierDao;

    @Resource
    private GoodsDao goodsDao;

    @Resource
    private ClassificationDao classificationDao;

    private JsonResult jsonResult = new JsonResult();

    @RequestMapping(value = "/show")
    @ResponseBody
    @Transactional
    public Supplier show(@RequestBody Map<String,Object> map) {
        Supplier supplier = supplierDao.selectById((Integer) map.get("id"));
        return supplier;
    }

    @RequestMapping(value = "/insertsupplier")
    @ResponseBody
    @Transactional
    public JsonResult insertUser(@RequestBody  Map<String, Supplier> map) {
        int flag = supplierDao.insert(map.get("supplier"));
        if (flag == 0) {
            jsonResult.setResult("false");
            return jsonResult;
        }
        else {
            jsonResult.setResult("true");
            return jsonResult;
        }
    }

    @RequestMapping(value = "/updatesupplier")
    @ResponseBody
    @Transactional
    public JsonResult updateUser(@RequestBody  Map<String, Supplier> map) {
        int flag = supplierDao.update(map.get("supplier"));
        if (flag == 0) {
            jsonResult.setResult("false");
            return jsonResult;
        }
        else {
            jsonResult.setResult("true");
            return jsonResult;
        }
    }

    @RequestMapping(value = "/deletesupplier")
    @ResponseBody
    @Transactional
    public JsonResult deleteUser(@RequestBody Map<String,Object> map) {
        int flag = supplierDao.delete((Integer) map.get("id"));
        if (flag == 0) {
            jsonResult.setResult("false");
            return jsonResult;
        }
        else {
            jsonResult.setResult("true");
            return jsonResult;
        }
    }

    @RequestMapping(value = "/showAll")
    @ResponseBody
    @Transactional
    public List<CompositeSupplier> showAll() {
        List<Supplier> suppliers = new ArrayList<Supplier>();
        List<CompositeSupplier> compositeSuppliers = new ArrayList<CompositeSupplier>();
        suppliers = supplierDao.selectAll();
        for (Supplier supplier:suppliers
             ) {
            CompositeSupplier tmp = new CompositeSupplier();
            tmp.setSupplierName(supplier.getName());
            tmp.setSupplierId(supplier.getId());

            User manager =supplierDao.selectByManageId(supplier.getId());
            tmp.setManager(manager);

            List<ClassifiedGoods> classifiedGoodslist =new ArrayList<ClassifiedGoods>();
            List<Goods> SuppliedGoods =supplierDao.selectGoodsBySupplierId(supplier.getId());
            for (Goods goods:SuppliedGoods
                 ) {
                ClassifiedGoods classifiedGoods =new ClassifiedGoods();
                Classification classification = classificationDao.selectById(goods.getClassificationId());
                classifiedGoods.setGoods(goods);
                classifiedGoods.setClassification(classification.getName());
                classifiedGoods.setSupplierId(supplier.getId());
                classifiedGoods.setSupllierName(supplier.getName());
                classifiedGoodslist.add(classifiedGoods);
            }
            tmp.setGoodsList(classifiedGoodslist);
            compositeSuppliers.add(tmp);
        }
        return compositeSuppliers;
    }

    /*根据供应商id查找管理员*/
    @RequestMapping(value = "/showManagerBySupplier")
    @ResponseBody
    @Transactional
    public User showManagerBySupplier(@RequestBody Map<String,Integer>map){
        User manager =supplierDao.selectByManageId(map.get("id"));
        return manager;
    }
/*商品供应*/
    @RequestMapping(value = "/supplyGoods")
    @ResponseBody
    @Transactional
    public JsonResult supplyGoods(@RequestBody Map<String,Object>map){
        int flag;
        int stock =goodsDao.selectById((Integer) map.get("goodsID")).getStock();
        if(stock==0){
            flag=supplierDao.supplyGoods((Integer)map.get("supplierID"),(Integer)map.get("goodsID"),(Integer)map.get("number"));
        }else{
            flag=0;
            flag=supplierDao.supplyGoods((Integer)map.get("supplierID"),(Integer)map.get("goodsID"),(Integer)map.get("number"));
            map.put("number",stock+(Integer) map.get("number"));
            flag=goodsDao.updateNum((Integer)map.get("goodsID"),(Integer) map.get("number"));
        }
        if (flag == 0) {
            jsonResult.setResult("false");
            return jsonResult;
        }
        else {
            jsonResult.setResult("true");
            return jsonResult;
        }
    }

    @RequestMapping(value="/findGoodsBysupllierID")
    @ResponseBody
    @Transactional
    public List<GoodsOfSupplier> findGoodsBysupllierID(@RequestBody Map<String,Object> map){
        List<GoodsOfSupplier> goodsOfSupplierList =new ArrayList<>();
        Integer id = (Integer) map.get("id");

        Supplier supplier=supplierDao.selectById(id.longValue());

        List<HashMap> goodsList=supplierDao.findGoodsOfSupplier(id.longValue());
        for (HashMap tmp: goodsList
             ) {
            GoodsOfSupplier goodsOfSupplier =new GoodsOfSupplier();

            goodsOfSupplier.setSupplierId(supplier.getId());
            goodsOfSupplier.setSupplierName(supplier.getName());

            Integer Goodsid = (Integer) tmp.get("id");

            goodsOfSupplier.setGoodsId(Goodsid.longValue());
            goodsOfSupplier.setGoodsName((String)tmp.get("name"));

            BigDecimal price = (BigDecimal) tmp.get("price");

            goodsOfSupplier.setGoodsPrice(  price.doubleValue());

            goodsOfSupplier.setNumber((Integer) tmp.get("number"));

            goodsOfSupplier.setClassification((String) tmp.get("classification"));

            goodsOfSupplierList.add(goodsOfSupplier);

        }

        return goodsOfSupplierList;
    }
}
